import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from "vue-router";
import { ElMessage } from 'element-plus'
const history = createWebHashHistory()
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: () => import('../view/login/login.vue'),
    meta: { title: '登录' },
  },
  {
    path: '/index',
    component: () => import('../view/index/index.vue'),
    redirect: '/test',
    meta: { title: '首页', isAuth: true },
    children: [
      {
        path: '/test',
        component: () => import('../view/test/test.vue'),
        meta: { title: '考试', isAuth: true },
      },
      {
        path: '/Analyse',
        component: () => import('../view/test/Analyse.vue'),
        meta: { title: '考试分析', isAuth: true },
      },
      {
        path: '/createTest',
        component: () => import('../view/test/createTest.vue'),
        meta: { title: '添加考试', isAuth: true },
      },
      {
        path: '/exam',
        component: () => import('../view/exam/exam.vue'),
        meta: { title: '阅卷', isAuth: true },
      },
      {
        path: '/review',
        component: () => import('../view/exam/review.vue'),
        meta: { title: '阅卷', isAuth: true },
      },
      {
        path: '/subjects',
        component: () => import('../view/subjects/subjects.vue'),
        meta: { title: '试卷', isAuth: true },
      },
      {
        path: '/subjectsAdd',
        name: 'subjectsAdd',
        component: () => import('../view/subjects/subjectsAdd.vue'),
        meta: { title: '创建试卷', isAuth: true },
      },
      {
        path: '/databaselist',
        component: () => import('../view/databaselist/databaselist.vue'),
        meta: { title: '题库', isAuth: true },
      },

      {
        path: '/databasequestionlist',
        component: () =>
          import('../view/databasequestionlist/databasequestionlist.vue'),
        meta: { title: '试题列表', isAuth: true },
      },
      {
        path: '/department',
        component: () => import('../view/department/department.vue'),
        meta: { title: '部门', isAuth: true },
      },
      {
        path: '/teacher',
        component: () => import('../view/teacher/teacher.vue'),
        meta: { title: '师资', isAuth: true },
      },
      {
        path: '/classes',
        component: () => import('../view/classes/classes.vue'),
        meta: { title: '班级', isAuth: true },
      },
      {
        path: '/student',
        component: () => import('../view/student/student.vue'),
        meta: { title: '学员', isAuth: true },
      },
      {
        path: '/role',
        component: () => import('../view/role/role.vue'),
        meta: { title: '角色', isAuth: true },
      },
      // 学生端------------------------------------------------------------------------------------------------
      {
        path: '/stutest',
        component: () => import('../view/stutest/stutest.vue'),
      },
     
      // 设置
      {
        path: '/set',
        component: () => import('../view/set/index.vue'),
      },
    ],
  },
  {
    path: '/examprepare',
    name:'examprepare',
    component: () => import('../view/stutest/examprepare.vue'),
  },
  {
    path: '/examresults',
    name:'examresults',
    component: () => import('../view/stutest/examresults.vue'),
  },
  {
    path: '/stuexam',
    name:'stuexam',
    component: () => import('../view/stutest/stuexam.vue'),
  },
  {
    path: '/examine',
    name:'examine',
    component: () => import('../view/stutest/examine.vue'),
  },
];
const router = createRouter({
  history,
  routes
})
// 全局前置守卫，初始化时执行，每次路由切换前执行
router.beforeEach((to, from, next) => { 
  if (to.meta.isAuth) {
    if (sessionStorage.getItem('token') == null) {
      ElMessage.error('暂无权限查看,请先登录')
      router.push('/')
    } else {
      next()//执行
    }
  } else {
    next()
  }

})

// 全局后置守卫，初始化时执行，每次路由切换后执行
router.afterEach((to, from, next) => {
  if (to.meta.title) {
    (document.title as any) = to.meta.title //修改网页的title
  } else {
    document.title = "乐柠考试"
  }
})
export default router